<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

	<xsl:param name="unit"/>
	<xsl:param name="date"/>
	<xsl:variable name="photoWidth" select="'48mm'"/>

	<xsl:template match="/">
		<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml">
			<fo:layout-master-set>
				<fo:simple-page-master master-name="titlePage" page-width="11in" page-height="8.5in">
					<fo:region-body margin-top="0.5in" margin-bottom="1in" margin-left="0.5in" margin-right="0.5in" />
				</fo:simple-page-master>
				<fo:simple-page-master master-name="contentPages" page-width="11in" page-height="8.5in">
					<fo:region-body margin-top="0.5in" margin-bottom="0.5in" margin-left="0.5in" margin-right="0.5in"/>
					<fo:region-before region-name="contentHeader" extent="0.4in" />
					<fo:region-after region-name="contentFooter" extent="0.4in" />
				</fo:simple-page-master>
			</fo:layout-master-set>

			<fo:page-sequence master-reference="contentPages" initial-page-number="1" format="1">
				<fo:static-content flow-name="contentHeader">
					<fo:block display-align="after" text-align="center">
						<xsl:value-of select="$unit"/>
					</fo:block>
					<fo:block display-align="after" text-align="center">
						<xsl:value-of select="$date"/>
					</fo:block>
				</fo:static-content>
				<fo:static-content flow-name="contentFooter">
					<fo:block text-align="center">
						For church use only
					</fo:block>
					<fo:block text-align="center">
						Page <fo:page-number />
					</fo:block>
				</fo:static-content>
				<fo:flow font-family="Times New Roman, serif" font-size="8pt" flow-name="xsl-region-body">

					<xsl:apply-templates select="families"/>

				</fo:flow>
			</fo:page-sequence>

		</fo:root>
	</xsl:template>

	<xsl:attribute-set name="FamilyCell">
		<xsl:attribute name="padding">2mm</xsl:attribute>
		<xsl:attribute name="keep-together">always</xsl:attribute>
		<xsl:attribute name="text-align">center</xsl:attribute>
	</xsl:attribute-set>

	<xsl:template match="families">
		<fo:table table-layout="fixed">
			<fo:table-column column-number="1" column-width="0.25in"/>
			<fo:table-column column-number="2" number-columns-repeated="3" column-width="proportional-column-width(1)"/>
			<fo:table-body>
				<xsl:for-each select="family[position() mod 3 = 1]">
					<xsl:variable name="pos" select="position()"/>
					<fo:table-row keep-together="always">
						<fo:table-cell/>
						<xsl:apply-templates select="."/>
						<xsl:apply-templates select="following-sibling::family[1]"/>
						<xsl:apply-templates select="following-sibling::family[2]"/>
					</fo:table-row>
				</xsl:for-each>
			</fo:table-body>
		</fo:table>
	</xsl:template>

	<xsl:template match="family">
		<fo:table-cell xsl:use-attribute-sets="FamilyCell">
			<xsl:if test="position() mod 3 = 1">
				<xsl:attribute name="starts-row">true</xsl:attribute>
			</xsl:if>
			<fo:block>
				<!--<fo:inline-container>-->
					<fo:table table-layout="fixed" width="66mm" text-align="start">
						<fo:table-column column-number="1" column-width="18mm"/>
						<fo:table-column column-number="2" column-width="{$photoWidth}"/>
						<fo:table-body>
							<fo:table-row keep-together="always" keep-with-next="always">
								<fo:table-cell number-columns-spanned="2" vertical-align="middle">
									<xsl:if test="not(@photo)">
										<xsl:attribute name="border">solid black .5mm</xsl:attribute>
									</xsl:if>
									<fo:block>
									<xsl:choose>
										<xsl:when test="@photo">
											<fo:external-graphic src="url({@photo})" content-width="{$photoWidth}"/>
										</xsl:when>
										<xsl:otherwise>
											<fo:block text-align="center"
													padding-before="20mm" padding-after="20mm">
												[no photo]
											</fo:block>
										</xsl:otherwise>
									</xsl:choose>
									</fo:block>
								</fo:table-cell>
							</fo:table-row>
							<fo:table-row>
								<fo:table-cell padding-end="2mm" number-rows-spanned="2">
									<!-- last name -->
									<fo:block text-decoration="underline">
										<xsl:value-of select="@surname"/>
									</fo:block>
									<fo:block>
										<!-- family phone number -->
										<xsl:value-of select="@phone"/>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell>
									<!-- first names (and unique cell phone #'s) -->
									<fo:block>
										<xsl:for-each select="member">
											<fo:inline>
												<xsl:if test="@dependent = 'False'">
													<xsl:attribute name="font-weight">bold</xsl:attribute>
												</xsl:if>
												<xsl:value-of select="@name"/>
											</fo:inline>
											<xsl:if test="@phone">
												<xsl:text> (</xsl:text>
												<xsl:value-of select="@phone"/>
												<xsl:text>) </xsl:text>
											</xsl:if>
											<xsl:if test="position() &lt; last() and last() > 2">
												<xsl:text>, </xsl:text>
											</xsl:if>
											<xsl:if test="position() = last() - 1">
												<xsl:text> and </xsl:text>
											</xsl:if>
										</xsl:for-each>
									</fo:block>
								</fo:table-cell>
							</fo:table-row>
							<fo:table-row>
								<fo:table-cell>
									<fo:block>
										<xsl:value-of select="@address"/>
										<xsl:if test="@apartment">
											<xsl:text> #</xsl:text>
											<xsl:value-of select="@apartment"/>
										</xsl:if>
										<xsl:if test="@city and not(@state or @zip)">
											<fo:inline padding-left="4mm">
												<xsl:text> </xsl:text>
												<xsl:value-of select="@city"/>
											</fo:inline>
										</xsl:if>
									</fo:block>
									<xsl:if test="@state or @zip">
										<fo:block>
											<xsl:if test="@city">
												<xsl:text> </xsl:text>
												<xsl:value-of select="@city"/>
												<xsl:text>, </xsl:text>
											</xsl:if>
											<xsl:value-of select="@state"/>
											<xsl:text> </xsl:text>
											<xsl:value-of select="@zip"/>
										</fo:block>
									</xsl:if>
								</fo:table-cell>
							</fo:table-row>
						</fo:table-body>
					</fo:table>
				<!--</fo:inline-container>-->
			</fo:block>
		</fo:table-cell>
	</xsl:template>
</xsl:stylesheet>
